package com.mf.framework.common.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 分页实体
 * User: zhaoming
 * Date: 13-5-9
 * Time: 下午4:14
 * To change this template use File | Settings | File Templates.
 */
public class Page<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final int DEFAULT_PAGESIZE = 10;

    /**
     * 当前页
     */
    private int pageNum;
    /**
     * 每页多少项
     */
    private int pageSize;
    /**
     * 总页数
     */
    private int total;
    /**
     * 总项数
     */
    private int records;
    /**
     * 排序参数
     */
    private LinkedHashMap<String, String> sortItemMap;

    /**
     * 数据列表
     */
    private List<T> content;

    public LinkedHashMap<String, String> getSortItemMap() {
        return sortItemMap;
    }

    public void setSortItemMap(LinkedHashMap<String, String> sortItemMap) {
        this.sortItemMap = sortItemMap;
    }

    public Page() {
        this(1, 0, DEFAULT_PAGESIZE);
    }

    public Page(int pageNum) {
        this(pageNum, 0, DEFAULT_PAGESIZE);
    }

    public Page(int pageNum, int pageSize) {
        this(pageNum, 0, pageSize);
    }

    public Page(int pageNum, int records, int pageSize) {
        if (pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize <= 0) {
            pageSize = DEFAULT_PAGESIZE;
        }
        if (records < 0) {
            records = 0;
        }
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.records = records;
        this.total = (int) Math.ceil(((double) records / (double) pageSize));
        this.content = new ArrayList<T>(pageSize);
    }

    public int getStartIndex() {
        return (pageNum - 1) * pageSize + 1;
    }

    public int getEndIndex() {
        int end = pageNum * pageSize;
        if (end > records) {
            end = records;
        }
        return end;
    }

    public boolean addAll(Collection<? extends T> c) {
        return content.addAll(c);
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public int getRecords() {
        return records;
    }

    public void setRecords(int records) {
        this.records = records;
    }

    public List<T> getContent() {
        return content;
    }

    public void setContent(List<T> content) {
        this.content = content;
    }
}
